package reggie.ruiji.except;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import reggie.ruiji.common.R;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations ={ Controller.class, RestController.class})
@ResponseBody
@Slf4j
public class Eeception
{
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> except(SQLIntegrityConstraintViolationException e)
    {
        log.error(e.getMessage());
        if(e.getMessage().contains("Duplicate entry"))
        {
            String []spilt=e.getMessage().split(" ");
            String S=spilt[2]+"已存在";
            return R.error(S);
        }
        return R.error("未知错误");
    }


    @ExceptionHandler(Customexception.class)
    public R<String> except(Customexception e)
    {
        log.error(e.getMessage());//该分类下有菜品或套餐，不能删除

        return R.error(e.getMessage());
    }
}
